#########################################################################
# Time-stamp: <03/12/19 14:30:58 ostolop>
#
# K-groups Clustering dynamic data routines
#########################################################################
package EP::EPC::Interface::Clustering_K_groups;

use strict;

use EP::Legacy::EPCLUST;
use EP::EPC::Interface;

use EP::Config;

use XML::LibXSLT;

sub newDD {
  my $proto = shift;
  my $ep_query_doc = shift;
  my $query = shift;
  my $DDD = shift;

  my $class = ref($proto) || $proto;
  my $self  = {};
  bless $self, $class;
  $self->initialize($ep_query_doc, $query, $DDD);
  return $self;
}

sub initialize {
  my $self = shift;
  $self->{epc_query} = shift;
  $self->{query}     = shift;
  $self->{DDD}       = shift;

  return;
}

#sub list_datasets {
#  my $self = shift;
#  my $folder = shift;

#  return $self->{DDD}->list_datasets ( $folder, "HierClustHACK" );
#}

sub list_clustering_distances {
  my $self = shift;

  my $dist = &EP::Common::DB::get_dynamic_data("clust_dist", 1);

  my $dl = &EP::EPC::Interface::DD::dynamic_list( $dist );
  return $dl->findnodes('/*');
}


sub list_color_schemes {
  my $self = shift;

  my ($scheme);
  my ($count) = 0;

  &EP::Legacy::EPCLUST::define_color_choices();

  foreach my $color_scheme ( @{ @EP::Legacy::EPCLUST::GLOBAL_IMG_CHOICES } ) {
    $scheme->{$count}{key} = $count;
    $scheme->{$count}{value} = $$color_scheme[1];
    $count++;
  }

  my $dl = &EP::EPC::Interface::DD::dynamic_list( $scheme );
  return $dl->findnodes('/*');
}

sub create_js_color_schemes {
  my $self = shift;
  my ($image,$field) = split ',', shift;

  my ($js);
  my ($count) = 0;

  EP::Legacy::EPCLUST::define_color_choices();

  foreach my $color_scheme ( @{ @EP::Legacy::EPCLUST::GLOBAL_IMG_CHOICES } ) {
    $js .= <<__EOF__;
ColorScheme[$count] = new Image();
ColorScheme[$count].src = "$$color_scheme[0]";
ColorScheme[$count].desc = "$$color_scheme[1]";
__EOF__
    ;
    $count++;
  }

  my $color_scheme_js = <<__EOF__;
    var ColorScheme = new Array($count);

$js

function nextScheme () {
	var i = document.$field.selectedIndex;		
	selectScheme ( (i + 1) % ( ColorScheme.length ) );
}

function prevScheme () {
	var i = document.$field.selectedIndex;		
	selectScheme ( (i - 1 + ColorScheme.length ) % ( ColorScheme.length ) );
}

function selectScheme ( index ) {
	document.$image.src = ColorScheme[index].src;
	document.$field.selectedIndex = index;
}
__EOF__
;

  return $color_scheme_js;
}

1;
